package net.xxcxcxcx.xc_enhancedmob.enhanced.level;

import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.xxcxcxcx.xc_enhancedmob.component.EnhancedBaseComponent;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import static net.xxcxcxcx.xc_enhancedmob.EnhancedMobMod.StartTest;

public class Level {
    private static final List<EntityAttribute> Attributeslist = getAttributeslist();
    private static final List<UUID> UUIDS = getUUIDlist();

    public static List<EntityAttribute> getAttributeslist() {
        List<EntityAttribute> list = new ArrayList<>();
        list.add(EntityAttributes.GENERIC_MAX_HEALTH);//最大生命值
        list.add(EntityAttributes.GENERIC_ARMOR);//盔甲值
        list.add(EntityAttributes.GENERIC_ARMOR_TOUGHNESS);//盔甲韧性
        list.add(EntityAttributes.GENERIC_ATTACK_DAMAGE);//攻击伤害
        list.add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);//攻击击退距离
        list.add(EntityAttributes.GENERIC_ATTACK_SPEED);//攻速
        list.add(EntityAttributes.GENERIC_MOVEMENT_SPEED);//移动速度
        list.add(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE);//击退抗性
        list.add(EntityAttributes.GENERIC_FOLLOW_RANGE);//索敌距离
        return list;
    }

    public static List<UUID> getUUIDlist() {
        List<UUID> list = new ArrayList<>();
        list.add(UUID.fromString("e048c326-c72d-c395-bb4d-312cd1629f69"));//最大生命值
        list.add(UUID.fromString("7cd8a47a-6e18-2fb0-c631-478e7a75ef7c"));//盔甲值
        list.add(UUID.fromString("e6560344-920a-3e31-4f2f-baa3772c43e4"));//盔甲韧性
        list.add(UUID.fromString("06618dd5-73e2-dca1-2a3c-f0fe0c21e499"));//攻击伤害
        list.add(UUID.fromString("25ba5723-0bf4-bce9-09b5-afcc422cd00c"));//攻击击退距离
        list.add(UUID.fromString("7e234d1f-f98a-9ea4-f23e-2636b49988eb"));//攻速
        list.add(UUID.fromString("aa6848c0-774a-4932-e8b4-1ccb82ba0ddd"));//移动速度
        list.add(UUID.fromString("ddccf548-35d6-1a82-ab50-2a037dae4cdb"));//击退抗性
        list.add(UUID.fromString("c7f96447-67ae-8629-6dd9-1070fa413efe"));//索敌距离
        return list;
    }

    public static void increaseAttribute(MobEntity mobEntity, int level) {
        for (int i = 0; i < Attributeslist.size(); i++) {
            EntityAttributeModifier modifier = getEntityAttributeModifier(Attributeslist.get(i), level);
            if (modifier == null) continue;
            EntityAttributeInstance entityAttributeInstance = mobEntity.getAttributeInstance(Attributeslist.get(i));
            if (entityAttributeInstance == null) continue;
            if (!entityAttributeInstance.hasModifier(modifier))
                entityAttributeInstance.addPersistentModifier(modifier);
        }
    }

    public static void random(EnhancedBaseComponent component) {
        if (StartTest) {
            component.setMobLevel(30);
            return;
        }
        Random random = new Random();
        int maxlevel = 100;
        int minlevel = 1;
        switch (component.getRarity()) {
            case Legend:
                maxlevel += 1000;
                minlevel += 500;
            case Epic:
                maxlevel += 500;
                minlevel += 300;
            case Ingenuity:
                maxlevel += 200;
                minlevel += 100;
            case Excellent:
                maxlevel += 100;
                minlevel += 50;
            case Outstanding:
                maxlevel += 50;
                minlevel += 50;
            case Good:
                maxlevel += 50;
        }
        int value = random.nextInt(maxlevel - minlevel + 1) + minlevel;
        component.setMobLevel(value);
    }

    public static Text getText(int level) {
        LiteralText text = new LiteralText("LV " + level);
        if (level > 1000) {
            text.formatted(Formatting.RED);
        } else if (level > 500) {
            text.formatted(Formatting.GOLD);
        } else if (level > 300) {
            text.formatted(Formatting.DARK_PURPLE);
        } else if (level > 100) {
            text.formatted(Formatting.BLUE);
        } else if (level > 50) {
            text.formatted(Formatting.GREEN);
        } else if (level > 10) {
            text.formatted(Formatting.WHITE);
        } else {
            text.formatted(Formatting.GRAY);
        }
        return text;
    }

    @Nullable
    public static EntityAttributeModifier getEntityAttributeModifier(EntityAttribute entityAttribute, int level) {
        int num = -1;
        float count = 0;
        if (EntityAttributes.GENERIC_MAX_HEALTH.equals(entityAttribute)) {
            num = 0;
            if (level <= 10) {
                count = 0;
            } else if (level <= 50) {
                count = 0.5f * (level - 10);
            } else if (level <= 100) {
                count = 20 + 0.8f * (level - 50);
            } else if (level <= 300) {
                count = 60 + 1f * (level - 100);
            } else if (level <= 500) {
                count = 260 + 1f * (level - 300);
            } else if (level <= 1000) {
                count = 460 + 2f * (level - 500);
            } else {
                count = 1460 + 3f * (level - 1000);
            }
        } else if (EntityAttributes.GENERIC_ARMOR.equals(entityAttribute)) {
            num = 1;
            if (level <= 10) {
                count = 0.1f * level;
            } else if (level <= 50) {
                count = 1 + 0.1f * (level - 10);
            } else if (level <= 100) {
                count = 5 + 0.2f * (level - 50);
            } else if (level <= 300) {
                count = 15 + 0.1f * (level - 100);
            } else if (level <= 500) {
                count = 35 + 0.2f * (level - 300);
            } else {
                count = 75;
            }
        } else if (EntityAttributes.GENERIC_ARMOR_TOUGHNESS.equals(entityAttribute)) {
            num = 2;
            if (level <= 50) {
                count = 0;
            } else if (level <= 100) {
                count = 0.05f * (level - 50);
            } else if (level <= 300) {
                count = 2.5f + 0.02f * (level - 100);
            } else if (level <= 500) {
                count = 6.5f + 0.05f * (level - 300);
            } else {
                count = 16.5f;
            }
        } else if (EntityAttributes.GENERIC_ATTACK_DAMAGE.equals(entityAttribute)) {
            num = 3;
        } else if (EntityAttributes.GENERIC_ATTACK_KNOCKBACK.equals(entityAttribute)) {
            num = 4;
        } else if (EntityAttributes.GENERIC_ATTACK_SPEED.equals(entityAttribute)) {
            num = 5;
        } else if (EntityAttributes.GENERIC_MOVEMENT_SPEED.equals(entityAttribute)) {
            num = 6;
            if (level <= 50) {
                count = 0;
            } else if (level <= 100) {
                count = 0.0005f * (level - 50);
            } else if (level <= 300) {
                count = 0.025f + 0.0002f * (level - 100);
            } else {
                count = 0.065f;
            }
        } else if (EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE.equals(entityAttribute)) {
            num = 7;
        } else if (EntityAttributes.GENERIC_FOLLOW_RANGE.equals(entityAttribute)) {
            num = 8;
        }
        if (num == -1 || count == 0) return null;
        return new EntityAttributeModifier(UUIDS.get(num), "LEVEL_" + num, count, EntityAttributeModifier.Operation.fromId(0));
    }
}
